package com.robot.log.service;

import com.robot.log.dao.AgentLogDao;
import com.robot.log.pojo.AgentLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * agentLog服务层
 *
 * @author wuyunbin
 */
@Service
public class AgentLogService {
    @Autowired
    private AgentLogDao agentLogDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<AgentLog> findAll() {
        return agentLogDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<AgentLog> findSearch(Map whereMap, int page, int size) {
        Specification<AgentLog> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return agentLogDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<AgentLog> findSearch(Map whereMap) {
        Specification<AgentLog> specification = createSpecification(whereMap);
        return agentLogDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public AgentLog findById(String id) {
        return agentLogDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param agentLog
     */
    public void add(AgentLog agentLog) {
        //雪花分布式ID生成器
        agentLog.setId(idWorker.nextId() + "");
        agentLogDao.save(agentLog);
    }

    /**
     * 修改
     *
     * @param agentLog
     */
    public void update(AgentLog agentLog) {
        agentLogDao.save(agentLog);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        agentLogDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<AgentLog> createSpecification(Map searchMap) {
        return (Specification<AgentLog>) (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            // id
            String id = "id";
            if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
            }
            // content
            String content = "content";
            if (searchMap.get(content) != null && !"".equals(searchMap.get(content))) {
                predicateList.add(cb.like(root.get(content).as(String.class), "%" + (String) searchMap.get(content) + "%"));
            }
            //剔除软删除数据
            String deletedAt = "deletedAt";
            predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(Date.class)));

            return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
        };
    }

}
